<template>
    <div id="disk">
		
		<div style="width: 100%;height: 50px;padding: 10px">
			 <Button type="primary" @click="modal1 = true" style="float: right;">挂载NFS磁盘</Button>
			  <h2 style="text-align: center;padding-left: 180px;">管理NFS挂载磁盘</h2>
		</div>
		
		
		
      
    <Modal
        v-model="modal1"
        title="NFS连接"
        @on-ok="ok"
        @on-cancel="cancel">
      <div >
          NFS服务IP地址： <Input v-model="nfsIp" placeholder="XXX.XXX.XXX.XXX" style="width: 300px;padding-top:10px;"  /></br>
       NFS共享路径：<Input v-model="nfspath" placeholder="/home" style="width: 300px" />
      </div>
    </Modal>

    <ul v-for="(item,index) in arr" class="ullist" @contextmenu="showMenu(index)">
        <vue-context-menu :contextMenuData="contextMenuData" @deleted="deleted" @NewFile="NewFile"></vue-context-menu>
       <div style="padding:20px" @dblclick="dbclick(index)">
            <img src="/src/renderer/assets/icon/6.png" style="width:70px;height:70px;float:left;">
            <div style="margin-top:10px;user-select: none;">
                <h2 >{{item.nfsip}}</h2>
                <h4 >文件夹名称：{{item.name}}</h4>
            </div>
       </div>
       </ul>
    </div>
</template>
<script>
var exec = require("child_process");
var fs = require('fs')
export default {
    data(){
        return{
            arr:[],
            list:[],
            modal1: false,
            nfsIp:'',
            nfspath:'',
            contextIndex:'',    
            contextMenuData: {
					menuName: 'demo',
					//菜单显示的位置
					axis: {
						x: null,
						y: null
					},
					//菜单选项
					menulists: [{
						fnHandler: 'deleted', //绑定事件
						btnName: '删除', //菜单名称
					}, {
						fnHandler: 'NewFile',
						btnName: '重命名'
					}]
				},
        }
    },
    mounted(){
         // 读取配置文件 已经保存的NFS磁盘
            fs.readFile('./NFSconfig.ini', 'utf-8', (err, data)=> {
                if (err) {
                    throw err;
                }
                // 读取配置文件 并转换成json数组
               var arrjson = eval(data)
                this.arr = arrjson
                console.log(this.arr)
            });
    },
    methods:{
        ok () {
            var obj = []
           var arr = this.arr
           
        //IP
        var nfsIp = this.nfsIp
        // 路径
        var nfspath = this.nfspath
        const dirName = nfspath.substring(nfspath.lastIndexOf('/')+1)
        console.log('文件夹名称：'+dirName)
        // 创建文件夹
            fs.mkdir('/home/' + dirName, function(err,data) {
                if (err) {
                    throw err;
                }
                console.log(data)
            });

        // 挂载NFS命令
        // zf
         var cmd = 'sudo mount -t nfs '+nfsIp+':/'+nfspath+' /home/'+ dirName;
        exec.exec(cmd, function(error, stdout, stderr) {
            if(error){
            console.error(error);
            return;
        }
        else{
            console.log(stdout);
            
        }
        });

        // // 利用FS模块 获取NFS目录信息
        // var readDir = fs.readdirSync('/home/'+dirName,'utf-8');
        // console.log(readDir)
        // //将该目录渲染到list数组里面
        // this.list = readDir

        obj = [{
               'name':dirName,
               'nfsip':nfsIp
           }]

          this.arr =  arr.concat(obj)
            console.log(this.arr)

            // 将NFS数组转换成字符串
            // 写入NFSconfig.ini 配置文件中
            console.log(JSON.stringify(this.arr))
            fs.writeFile('./NFSconfig.ini', JSON.stringify(this.arr), (err)=> {
                if (err) {
                    throw err;
                }
                console.log('Saved.');
            });

            // 写入成功后读取测试
            fs.readFile('./NFSconfig.ini', 'utf-8', (err, data)=> {
                if (err) {
                    throw err;
                }
                // 读取配置文件 并转换成json数组
               var arrjson = eval(data)
               console.log(arrjson)
            });


             this.$Message.info('正在连接');
            },
            cancel () {
                this.$Message.info('取消连接');
            },
            // 双击标签事件
            dbclick(index){
               var nfs = this.arr[index]
               console.log(JSON.stringify(nfs))
               window.sessionStorage.setItem('NFS_Path',JSON.stringify(nfs))
               this.$router.push('/NFS_Home')
               
            },
             // 右键菜单
            showMenu(index) {
				event.preventDefault()
				var x = event.clientX
				var y = event.clientY
				// Get the current location
				this.contextMenuData.axis = {
					x,
					y
				}
                this.contextIndex = index
                console.log(index)
            },
            deleted(){
                const index = this.contextIndex
                console.log(index)

            this.arr.splice(index,index+1);
            console.log(this.arr)

            // 将NFS数组转换成字符串
            // 写入NFSconfig.ini 配置文件中
            console.log(JSON.stringify(this.arr))
            fs.writeFile('./NFSconfig.ini', JSON.stringify(this.arr), (err)=> {
                if (err) {
                    throw err;
                }
                console.log('写入成功');
            });

            // 写入成功后读取测试
            fs.readFile('./NFSconfig.ini', 'utf-8', (err, data)=> {
                if (err) {
                    throw err;
                }
                // 读取配置文件 并转换成json数组
               var arrjson = eval(data)
               console.log(arrjson)
            });
            }
    }
}
</script>

